package com.ddd.infrastructure.common.exception;

import com.ddd.framework.dto.Response;
import com.ddd.framework.enums.ResponseCode;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 *
 * @author guowm
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 拦截业务异常
     */
    @ExceptionHandler(DddBizException.class)
    public Response handleDddBizException(DddBizException e, HttpServletRequest request) {
        log.error("业务异常:{}, {}", e.getErrCode(), e.getMessage());
        return Response.failure(ResponseCode.FAIL.getCode(), e.getMessage());
    }
    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Response handleRuntimeException(RuntimeException e, HttpServletRequest request) {
        log.error("运行时异常:{}", e.getMessage());
        return Response.failure(ResponseCode.FAIL.getCode(), e.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public Response handleException(Exception e, HttpServletRequest request) {
        log.error("系统异常:{}", e.getMessage());
        return Response.failure(ResponseCode.FAIL.getCode(), e.getMessage());
    }
}
